//index.js
//获取应用实例
const app = getApp()
let Axios = require('../../../common/js/ajax.js'); //引入Axios方法
let WxValidate = require('../../../common/js/WxValidate.js'); //表单验证
let Mtils = require('../../../common/js/Mtils.min.js');
Mtils = Mtils.Mtils
Page({
	data: {
		openid: '',
		nickName: '',
		avatarUrl: '',
		curentNav: 2, //当前导航
		goodscore: '',
		order_id: 0,
		store_id: 0
	},
	//事件处理函数
	onLoad: function(e) {
		this.initValidate()
		this.uid = app.globalData.uid || ''
		this.Md5 = Mtils.security.hex_md5(this.uid) + app.globalData.secret;
		this.Md5 = Mtils.security.hex_md5(this.Md5);
		this.timesp = Mtils.now();
		this.setData({
			order_id: e.order_id,
			store_id: e.store_id
		})
		if (app.globalData.nickName && app.globalData.avatarUrl) {
			this.setData({
				nickName: app.globalData.nickName,
				avatarUrl: app.globalData.avatarUrl
			})
		}
	},
	onReady: function() {
		wx.setNavigationBarTitle({
			title: '回评'
		})
	},
	preventTouchMove: function() {

	},
	handleStatTap: function(e) { //星
		var id = e.currentTarget.dataset.id;
		this.setData({
			goodscore: id
		})
		if (id == 0) {
			this.setData({
				starg: !this.data.starg
			})
		} else {
			this.setData({
				starg: true
			})
		}
	},
	submit: function(e) {
		console.log(e.detail)
		var params = e.detail.value
		if (!this.WxValidate.checkForm(params)) {
			const error = this.WxValidate.errorList[0]
			this.showModal(error)
			return false
		}
		this.submitInfo(params);
	},
	submitInfo: function(e) {
		var _this = this
		var CONST = {
			uid: this.uid,
			time: this.timesp,
			sign: this.Md5,
			order_id: this.data.order_id,
			store_id: this.data.store_id,
			owner: 1
		}
		var obj = Object.assign(e, CONST);
		Axios.get(app.globalData.baseUrl + 'App/Store/Order/order_evaluate',
			obj, (res) => {
				console.log(res)
				if (res.status == 1) {
					wx.navigateTo({
						url: '/pages/shopOrder/shopOrder?culum=6&id=' + this.data.store_id
					})
				} else {
					_this.showModal(res)
				}
			})
	},
	showModal: function(error) {
		wx.showModal({
			content: error.msg,
			showCancel: false,
		})
	},
	initValidate: function() {
		/** 4-2(配置规则)*/
		const rules = {
			content: {
				required: true,
			},
			member_point: {
				required: true,
			},

		}
		// 验证字段的提示信息，若不传则调用默认的信息
		const messages = {
			content: {
				required: '请输入评价内容',
			},
			member_point: {
				required: '请给买家评分',
			}
		}
		// 创建实例对象
		this.WxValidate = new WxValidate.WxValidate(rules, messages)
		/**
		 * 也可以自定义验证规则
		 */
		// 		this.WxValidate.addMethod('assistance', (value, param) => {
		// 			return this.WxValidate.optional(value) || (value.length >= 1 && value.length <= 2)
		// 		}, '请勾选 《顺风男服务协议》')
	}

})
